<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30
 * Time: 13:49
 */

namespace app\index\controller;


use app\common\entity\Image;
use app\common\entity\ServiceInfo;
use app\common\entity\WithdrawRatio;
use app\common\shop\ArticleCateModel;
use think\Controller;
use think\Db;
use think\Request;
use app\common\entity\Article as art;

class Article extends Common
{
    /**
     * [文章列表]
     * @param
     * @return
     */
    public function articlelist( Request $request ){
        //article_id,category,title,content,create_time,pic,describe,cateid
        $art =art::field('*');
        $category = $request->post('category');
        if($category){
            $art->where('category',$category);
        }
        $cateid = $request->post('cateid');
        if($cateid){
            $art->where('cateid',$cateid);
        }
        $list = $art->where('status',1)
                ->order('sort asc,create_time desc')
                ->select()->toArray();
       
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$list]);
    }
    /**
     * [分类]
     * @param
     * @return
     */
    public function cate( Request $request ){
        $list = ArticleCateModel::order('id asc')->select()->toArray();
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$list]);
    }

    /**
     * [用户协议]
     * @param
     * @return
     */
    public function userAgreement( Request $request ){
        $info = art::where('category',3)->where('status',1)->order('sort asc,create_time desc')->find();
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$info]);
    }

    /**
     * [文章详情]
     * @param
     * @return
     */
    public function articleDetail( Request $request ){

        $article_id = $request->post('article_id',32);
        $info = art::where('article_id',$article_id)->find();
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$info]);
       // $list = \app\common\entity\Article::where('article_id', $article_id)->find();
    }

    #新闻公告列表
    public function artList(Request $request)
    {
        $list = \app\common\entity\Article::where('status', 1)->where('category', '=', 1)->order('create_time desc')->select();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }

    #新闻公告详情
    public function artInfo(Request $request)
    {

        $article_id = $request->get('article_id');
        $list = \app\common\entity\Article::where('article_id', $article_id)->find();
        if ($list) {
            $article = new \app\common\entity\Article();
//            dump( $list['content']);die;
            $list['content'] = $article->updImgUrl($list['content']);

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }

    #福多多公告弹窗
    public function indexArt()
    {
        $two_day = time() - 3600 * 48;
        $list = \app\common\entity\Article::whereTime('create_time', 'between', [$two_day, time()])->order('create_time desc')->find();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }

    #福多多轮播
    public function indexBanner()
    {
        $list = \app\common\entity\Article::where('category', 1)->select();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }
    }


    #获取客服
    public function getServiceInfo()
    {
        $list = ServiceInfo::find();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }

    #获取图片
    public function getImg()
    {
        $list = Image::where('types', 1)->select();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }
    #获取商城图片
    public function getShopImg()
    {
        $list = Image::where('types', 2)->select();
        if ($list) {

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }


    #获取服务条款
    public function getService()
    {
        $list = \app\common\entity\Article::where('article_id', 3)->find();
        $down_url = WithdrawRatio::where('key', 'app_down')->value('ratio');

        if ($list) {
            $article = new \app\common\entity\Article();
//            dump( $list['content']);die;
            $list['content'] = $article->updImgUrl($list['content']);

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list, 'down_url' => $down_url]);

        }

        return json(['code' => 1, 'message' => '获取失败']);
    }

    #交易规则
    public function shopRule()
    {
        $list = \app\common\entity\Article::where('article_id', 5)->find();
        if ($list) {
            $article = new \app\common\entity\Article();
//            dump( $list['content']);die;
            $list['content'] = $article->updImgUrl($list['content']);

            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);
        }
    }

    #获取广告图片
    public function getAdvert()
    {
        $list = Image::where('types',2)->where('click_num','>',0)->orderRand()->find();
        if ($list) {
            Image::where('id',$list['id'])->setDec('click_num');
            return json(['code' => 0, 'message' => '获取成功', 'info' => $list]);

        }

        return json(['code' => 1, 'message' => '暂无广告剩余次数']);
    }


}